﻿using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Views.Grid;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace IOModule
{
    public partial class FormIODebug : DevExpress.XtraEditors.XtraForm
    {
        List<InputStatus> inputGeneralStatuses;
        List<OutputStatus> outputGeneralStatuses;
        List<InputStatus> inputStandardStatuses;
        List<OutputStatus> outputStandardStatuses;
        public FormIODebug()
        {
            InitializeComponent();
        }

        private void timerStatusScan_Tick(object sender, EventArgs e)
        {
            foreach (InputStatus status in inputGeneralStatuses)
            {
                status.RefreshStatus();
            }
            foreach (OutputStatus status in outputGeneralStatuses)
            {
                status.RefreshStatus();
            }
            foreach (InputStatus status in inputStandardStatuses)
            {
                status.RefreshStatus();
            }
            foreach (OutputStatus status in outputStandardStatuses)
            {
                status.RefreshStatus();
            }
            gridControlGeneralInput.RefreshDataSource();
            gridControlGeneralOutput.RefreshDataSource();
            gridControlStandardInput.RefreshDataSource();
            gridControlStandardOutput.RefreshDataSource();

           
        }

        private void FormIODebug_Load(object sender, EventArgs e)
        {
            inputGeneralStatuses=new List<InputStatus>();
            outputGeneralStatuses=new List<OutputStatus>();
            inputStandardStatuses=new List<InputStatus>();
            outputStandardStatuses=new List<OutputStatus>();
            foreach (IOInputItem item in IOManager.ioSetting.listInputSettings)
            {
                InputStatus inputStatus = new InputStatus(item);
                inputGeneralStatuses.Add(inputStatus);
            }
            foreach (IOInputItem item in IOManager.ioSetting.listStandInputs)
            {
                InputStatus inputStatus = new InputStatus(item);
                inputStandardStatuses.Add(inputStatus);
            }
            foreach (IOOutputItem item in IOManager.ioSetting.listOutputSettings)
            {
                OutputStatus outputStatus = new OutputStatus(item);
                outputGeneralStatuses.Add(outputStatus);
            }
            foreach (IOOutputItem item in IOManager.ioSetting.listStandOutputs)
            {
                OutputStatus outputStatus = new OutputStatus(item);
                outputStandardStatuses.Add(outputStatus);
            }

            gridControlGeneralInput.DataSource= inputGeneralStatuses;
            gridControlGeneralOutput.DataSource = outputGeneralStatuses;
            gridControlStandardInput.DataSource = inputStandardStatuses;
            gridControlStandardOutput.DataSource = outputStandardStatuses;

            GridView gridViewProject = gridControlGeneralOutput.MainView as GridView;
            AddNewColGeneralOutput(gridViewProject, "ON");
            AddNewColGeneralOutput(gridViewProject, "OFF");
           

            GridView gridViewGlobal = gridControlStandardOutput.MainView as GridView;
            AddNewColStandardOutput(gridViewGlobal, "ON");
            AddNewColStandardOutput(gridViewGlobal, "OFF");


            timerStatusScan.Enabled = true;
        }
        public void AddNewColGeneralOutput(GridView gridView, string strColName)
        {
            RepositoryItemButtonEdit repositoryItemButton = new RepositoryItemButtonEdit();
            repositoryItemButton.ButtonsStyle = DevExpress.XtraEditors.Controls.BorderStyles.Flat;
            repositoryItemButton.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.Standard;
            repositoryItemButton.Buttons[0].Kind = DevExpress.XtraEditors.Controls.ButtonPredefines.Glyph;

            repositoryItemButton.Buttons[0].Caption = strColName;
            repositoryItemButton.TextEditStyle = TextEditStyles.HideTextEditor;
            gridView.Columns.AddVisible(strColName);

            gridView.Columns[strColName].ColumnEdit = repositoryItemButton;

            repositoryItemButton.Buttons[0].Click += GeneralOutputGridCtrolButton_Click; ;
        }

        private void GeneralOutputGridCtrolButton_Click(object sender, EventArgs e)
        {
            EditorButton editorButton = sender as EditorButton;
            GridView gridView = gridControlGeneralOutput.MainView as GridView;
            int iIndex = gridView.GetFocusedDataSourceRowIndex();
            if (iIndex > -1)
            {
                if (editorButton.Caption == "ON")
                {
                    IOManager.Output(outputGeneralStatuses[iIndex].OutputName).ON = true;
                }
                if (editorButton.Caption == "OFF")
                {
                    IOManager.Output(outputGeneralStatuses[iIndex].OutputName).ON = false;
                }
                //XtraMessageBox.Show("Row:" + iIndex.ToString() + "Click--" + "Action:" + editorButton.Caption);
            }


        }
        public void AddNewColStandardOutput(GridView gridView, string strColName)
        {
            RepositoryItemButtonEdit repositoryItemButton = new RepositoryItemButtonEdit();
            repositoryItemButton.ButtonsStyle = DevExpress.XtraEditors.Controls.BorderStyles.Flat;
            repositoryItemButton.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.Standard;
            repositoryItemButton.Buttons[0].Kind = DevExpress.XtraEditors.Controls.ButtonPredefines.Glyph;

            repositoryItemButton.Buttons[0].Caption = strColName;
            repositoryItemButton.TextEditStyle = TextEditStyles.HideTextEditor;
            gridView.Columns.AddVisible(strColName);

            gridView.Columns[strColName].ColumnEdit = repositoryItemButton;

            repositoryItemButton.Buttons[0].Click += StandardOutputGridCtrolButton_Click; ;
        }

        private void StandardOutputGridCtrolButton_Click(object sender, EventArgs e)
        {
            EditorButton editorButton = sender as EditorButton;
            GridView gridView = gridControlStandardOutput.MainView as GridView;
            int iIndex = gridView.GetFocusedDataSourceRowIndex();
            if (iIndex > -1)
            {
                if (editorButton.Caption == "ON")
                {
                    IOManager.ioSetting.dicStandOutputs[outputStandardStatuses[iIndex].OutputName].ON = true;
                }
                if (editorButton.Caption == "OFF")
                {
                    IOManager.ioSetting.dicStandOutputs[outputStandardStatuses[iIndex].OutputName].ON = false;
                }
                //XtraMessageBox.Show("Row:" + iIndex.ToString() + "Click--" + "Action:" + editorButton.Caption);
            }


        }
    }
}